package com.fengchao.util.wechat.commonality.rendermodel;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.fengchao.util.encrypt.Md5;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.google.common.base.Strings;
import com.google.gson.Gson;

/**
 * 发送红包时的参数
 *
 * @author xuri002
 */
public class RedPackParameter {
    private String nonce_str;//随机字符串，不长于32位  必填  String(32)
    private String sign;//签名 必填  String(32)
    private String mch_billno;//商户订单号  商户订单号（每个订单号必须唯一）组成： mch_id+yyyymmdd+10位一天内不能重复的数字。  必填  String(28)
    private String mch_id;//商户号  必填  String(32)
    private String sub_mch_id;//String(32)微信支付分配的子商户号，受理模式下必填   非必填
    private String wxappid;//公众账号appid  必填  String(32)
    private String nick_name;//提供方名称  必填  String(32)
    private String send_name;//商户名称  必填  String(32)
    private String re_openid;//用户openid  必填  String(32)
    private String total_amount;//付款金额(单位为分)  必填  整数
    private String min_value;//最小红包金额(单位为分)  必填  整数
    private String max_value;//最大红包金额(单位为分) 必填   整数  （ 最小金额等于最大金额： min_value=max_value =total_amount）
    private String total_num;//红包发放总人数 必填   整数
    private String wishing;//红包祝福语  必填
    private String client_ip;//调用接口的机器Ip地址  必填
    private String act_name;//String(32) 活动名称  必填
    private String remark;//String(256)备注信息  必填
    private String logo_imgurl;//String(128)商户logo的url   非必填
    private String share_content;//String(256)分享文案   非必填
    private String share_url;//String(128)分享链接   非必填
    private String share_imgurl;//String(128)分享的图片url   非必填

    public String getNonce_str() {
        return nonce_str;
    }

    public void setNonce_str(String nonce_str) {
        this.nonce_str = nonce_str;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getMch_billno() {
        return mch_billno;
    }

    public void setMch_billno(String mch_billno) {
        this.mch_billno = mch_billno;
    }

    public String getMch_id() {
        return mch_id;
    }

    public void setMch_id(String mch_id) {
        this.mch_id = mch_id;
    }

    public String getSub_mch_id() {
        return sub_mch_id;
    }

    public void setSub_mch_id(String sub_mch_id) {
        this.sub_mch_id = sub_mch_id;
    }

    public String getWxappid() {
        return wxappid;
    }

    public void setWxappid(String wxappid) {
        this.wxappid = wxappid;
    }

    public String getNick_name() {
        return nick_name;
    }

    public void setNick_name(String nick_name) {
        this.nick_name = nick_name;
    }

    public String getSend_name() {
        return send_name;
    }

    public void setSend_name(String send_name) {
        this.send_name = send_name;
    }

    public String getRe_openid() {
        return re_openid;
    }

    public void setRe_openid(String re_openid) {
        this.re_openid = re_openid;
    }

    public String getTotal_amount() {
        return total_amount;
    }

    public void setTotal_amount(String total_amount) {
        this.total_amount = total_amount;
    }

    public String getMin_value() {
        return min_value;
    }

    public void setMin_value(String min_value) {
        this.min_value = min_value;
    }

    public String getMax_value() {
        return max_value;
    }

    public void setMax_value(String max_value) {
        this.max_value = max_value;
    }

    public String getTotal_num() {
        return total_num;
    }

    public void setTotal_num(String total_num) {
        this.total_num = total_num;
    }

    public String getWishing() {
        return wishing;
    }

    public void setWishing(String wishing) {
        this.wishing = wishing;
    }

    public String getClient_ip() {
        return client_ip;
    }

    public void setClient_ip(String client_ip) {
        this.client_ip = client_ip;
    }

    public String getAct_name() {
        return act_name;
    }

    public void setAct_name(String act_name) {
        this.act_name = act_name;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getLogo_imgurl() {
        return logo_imgurl;
    }

    public void setLogo_imgurl(String logo_imgurl) {
        this.logo_imgurl = logo_imgurl;
    }

    public String getShare_content() {
        return share_content;
    }

    public void setShare_content(String share_content) {
        this.share_content = share_content;
    }

    public String getShare_url() {
        return share_url;
    }

    public void setShare_url(String share_url) {
        this.share_url = share_url;
    }

    public String getShare_imgurl() {
        return share_imgurl;
    }

    public void setShare_imgurl(String share_imgurl) {
        this.share_imgurl = share_imgurl;
    }

    @Override
    public String toString() {
        return "RedPackParameter [nonce_str=" + nonce_str + ", sign=" + sign
                + ", mch_billno=" + mch_billno + ", mch_id=" + mch_id
                + ", sub_mch_id=" + sub_mch_id + ", wxappid=" + wxappid
                + ", nick_name=" + nick_name + ", send_name=" + send_name
                + ", re_openid=" + re_openid + ", total_amount=" + total_amount
                + ", min_value=" + min_value + ", max_value=" + max_value
                + ", total_num=" + total_num + ", wishing=" + wishing
                + ", client_ip=" + client_ip + ", act_name=" + act_name
                + ", remark=" + remark + ", logo_imgurl=" + logo_imgurl
                + ", share_content=" + share_content + ", share_url="
                + share_url + ", share_imgurl=" + share_imgurl + "]";
    }


    /**
     * 生成签名
     *
     * @param apiSecretKey
     * @return 注意：签名要在其他属性设置完之后再生成
     */
    @SuppressWarnings("unchecked")
    public String generateSign(String apiSecretKey) {
        Gson gson = new Gson();
        String gsonStr = gson.toJson(this);
        Map<String, String> map = gson.fromJson(gsonStr, Map.class);
        Map<String, String> treeMap = new TreeMap<String, String>();
        for (String key : map.keySet()) {
            treeMap.put(key, map.get(key));
        }
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        for (String key : treeMap.keySet()) {
            nvps.add(new BasicNameValuePair(key, treeMap.get(key)));
        }
        String sign = "";
        try {
            sign = URLDecoder.decode(EntityUtils.toString(new UrlEncodedFormEntity(nvps, "UTF-8")), "UTF-8");
        } catch (Exception e) {
        }
        if (!Strings.isNullOrEmpty(sign)) {
            sign += "&key=" + apiSecretKey;
            sign = Md5.encoderToString(sign, "").toUpperCase();
        }
        return sign;
    }
}
